package com.sky.controller.admin;

import com.sky.annotation.Log;
import com.sky.enumeration.BusinessType;
import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.concurrent.ExecutionException;

@Slf4j
@RestController
@RequestMapping("/admin/report")
public class ReportConroller {
    @Autowired
    ReportService reportService;

    // 营业额统计
    @GetMapping("/turnoverStatistics")
    public Result turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("营业额统计：{}，{}", begin, end);

        TurnoverReportVO turnoverReportVO = reportService.turnoverStatistics(begin, end);

        return Result.success(turnoverReportVO);

    }

    // 用户统计
    @GetMapping("/userStatistics")
    public Result turnoverUserStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("营业额统计：{}，{}", begin, end);

        UserReportVO userReportVO = reportService.turnoverUserStatistics(begin, end);

        return Result.success(userReportVO);

    }

    // 订单统计
    @GetMapping("/ordersStatistics")
    public Result ordersStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("营业额统计：{}，{}", begin, end);

        OrderReportVO orderReportVO = reportService.ordersStatistics(begin, end);

        return Result.success(orderReportVO);

    }

    // 查询销量排名top10
    @GetMapping("/top10")
    public Result topTen(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("营业额统计：{}，{}", begin, end);

        SalesTop10ReportVO salesTop10ReportVO = reportService.topTen(begin, end);

        return Result.success(salesTop10ReportVO);

    }

    // 导出Excel报表
    @GetMapping("/export")
    @Log(title = "导出Excel报表",businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response) throws IOException, ExecutionException, InterruptedException {


        reportService.exportExcel( response);

    }
}
